package leetcode.剑指offer.后40;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/8/1 11:43
 * @description：https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof/
 */
public class 剑指_48_最长不含重复字符的子字符串 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.lengthOfLongestSubstring("pwwkew"));
    }

    class Solution {
        public int lengthOfLongestSubstring(String s) {
            HashMap<Character, Integer> map = new HashMap<>();
            int max = 0;
            int temp = 0;
            char[] chars = s.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                Integer j = map.getOrDefault(chars[i], -1);
                map.put(chars[i], i);
                temp = temp < i - j ? temp + 1 : i - j;
                max = Math.max(max, temp);
            }
            return max;
        }
    }

}
